package com.liyunc.socket.point;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

public class Client {

    public static void main(String[] args) throws IOException {
        final int port = 40000;
        final String host = "localhost";
        final SocketAddress address = new InetSocketAddress(host, port);

        String sendMsg = null;

        Socket socket = new Socket();
        socket.connect(address);
        System.out.println(socket);

        //系统输入
        BufferedReader userIn = new BufferedReader(new InputStreamReader(System.in));
        //从服务器读取信息
        BufferedReader socketRead =
            new BufferedReader(new InputStreamReader(socket.getInputStream()));
        //发送信息给服务器
        PrintWriter socketWrite = new PrintWriter(socket.getOutputStream());

        while (!"end".equals(sendMsg)) {
            //客户端输入
            System.out.print("multi.Client input : ");
            sendMsg = userIn.readLine();

            //写入socket
            socketWrite.println(sendMsg);
            socketWrite.flush();

            //从服务端接收信息
            String serverInfo = socketRead.readLine();
            System.out.println("server say : " + serverInfo);
        }
        userIn.close();
        socketRead.close();
        socketWrite.close();
        socket.close();
    }
}
